import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { start, close } from "@/plugins/nprogress";
import { getToken } from "@/plugins/auth";
const modules: any = import.meta.glob("./modules/**/*.ts", { eager: true });
const routeModuleList: RouteRecordRaw[] = [];

Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

const router = createRouter({
  history: createWebHistory(""),
  routes: [
    ...routeModuleList,
  ],
});

router.beforeEach((to, from, next) => {

  start()
  if(to.path == '/'){
    console.log(to.path )
      router.push('/dashbroad');
  }
  // if (
  //   getToken() &&
  //   (to.path === "/login" || to.path === "/home")
  // ) {
  //   router.push(to.path);
  // }
  next();
});

router.afterEach(() => {
  close();
});

export default router;
